package banwood.com.xiongfasy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import banwood.com.xiongfasy.BASE.BaseActivity;
import banwood.com.xiongfasy.BASE.GetMessage;
import banwood.com.xiongfasy.R;
import banwood.com.xiongfasy.adapter.BillAdapter;
import banwood.com.xiongfasy.bean.BillBean;
import banwood.com.xiongfasy.bean.BillDetail;
import banwood.com.xiongfasy.dialog.LoadingDialog;
import banwood.com.xiongfasy.http.GetAsynctask;
import banwood.com.xiongfasy.listener.ResultListener;
import banwood.com.xiongfasy.utils.LogUtils;
import butterknife.BindView;
import butterknife.OnClick;
import cn.shellinfo.serv.comm.ParamMap;


public class BillActivity extends BaseActivity {
    private static String TAG = "BillActivity";
    @BindView(R.id.bill_linear)
    LinearLayout billLinear;
    @BindView(R.id.bill_order)
    EditText billOrder;
    @BindView(R.id.bill_start)
    ImageView billStart;
    @BindView(R.id.bill_search_start)
    LinearLayout billSearchStart;
    @BindView(R.id.bill_listview)
    ListView mlistview;

    private List<BillBean> mDatas;
    private BillAdapter adapter;
    private LoadingDialog dialog;

    @Override
    protected int getLayoutId() {
        return R.layout.bill_activity;
    }

    @Override
    protected void initView() {
        settitle("账单");
        mDatas = new ArrayList<BillBean>();
        adapter = new BillAdapter(BillActivity.this, mDatas);
        mlistview.setAdapter(adapter);


        mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startQuery(mDatas.get(position).getOrder(),"账单详情");
            }
        });


    }

    @Override
    protected void initData() {
        dialog=new LoadingDialog(BillActivity.this,1);
        dialog.setMessage("正在加载数据...");
        dialog.show();
        ParamMap paramMap = new ParamMap();
        paramMap.put("SettleDate", GetMessage.getData());
        paramMap.put("SettleStartHour", "0");
        paramMap.put("SettleEndHour", "23");
        paramMap.put("ZIP", "1");

        new GetAsynctask(new ResultListener() {
            @Override
            public void getResult(ParamMap result) {
                dialog.dismiss();
                if (result != null) {
                   if(result.containsKey("detailRecords")){
                       getString(result.get("detailRecords").toString());
                   }else {
                       Toast.makeText(BillActivity.this, "今天没有产生账单", Toast.LENGTH_SHORT).show();
                   }

                } else {
                    Toast.makeText(BillActivity.this, "今天没有产生账单", Toast.LENGTH_SHORT).show();
                }
            }
        }, BillActivity.this, paramMap, "getBillQuery").execute(paramMap);


    }

    public void getString(String s) {
        String[] a = s.split("\\u005E\\u005E");
        for (int i = 0; i < a.length; i++) {
            String s2 = a[i];
            String[] b = s2.split("\\|");
            BillBean bean = new BillBean();
            bean.setUser(MyApplication.getUserName());
            bean.setType(b[0]);
            bean.setOrder(b[1]);
            bean.setTime(b[2]);
            bean.setMoney(b[3]);
            bean.setStatus(b[4]);
            bean.setCustomor(b[5]);
            bean.setBusiness(b[6]);
            //只显示成功支付的
            if(bean.getStatus().equals("0000")){
                mDatas.add(bean);
            }


        }

        adapter.notifyDataSetChanged();


    }


    @OnClick({R.id.bill_linear,R.id.bill_start})
    public void Onclick(View v){
        switch (v.getId()){
            case R.id.bill_linear:
                billLinear.setVisibility(View.GONE);
                billSearchStart.setVisibility(View.VISIBLE);
                break;
            case R.id.bill_start:
                if(!billOrder.getText().toString().trim().equals("") || billOrder.getText().toString().length()>0){
                    startQuery(billOrder.getText().toString(),"查询结果");
                }else{
                    Toast.makeText(BillActivity.this,"请输入流水号",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void startQuery(String msg, final String title) {
        dialog.setMessage("正在查询信息...");
        dialog.show();

        ParamMap paramMap=new ParamMap();
        paramMap.put("PayTypeID","ImmediatePay");
        paramMap.put("OrderNo",msg);
        paramMap.put("QueryDetail","1");

        new GetAsynctask(new ResultListener() {
            @Override
            public void getResult(ParamMap result) {
                dialog.dismiss();

                if(result!=null && result.getString("ReturnCode").equals("0000")){
                    LogUtils.d(TAG,result.toString());
                    String odrenInfo=result.getString("orderDetail");
                    Gson gson=new Gson();
                    BillDetail info=new BillDetail();
                    info=gson.fromJson(odrenInfo,BillDetail.class);
                    Intent intent=new Intent(BillActivity.this,BillDetailActivity.class);
                    intent.putExtra("title",title);
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("param",info);
                    intent.putExtras(bundle);
                    startActivity(intent);


                }else{
                    Intent intent=new Intent(BillActivity.this,QueryFailActivity.class);
                    startActivity(intent);
                }


            }
        },BillActivity.this,paramMap,"getQueryOrder").execute(paramMap);


    }



}
